//
//  BingdingMyBoxCarViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/8/12.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "BingdingMyBoxCarViewController.h"
#import "MineCarCell.h"

#define HEAD_VIEW_HEIGHT (95)
#define BOTTOM_BTN_HEGIHT (42)
#define BOTTOM_BTN_TOPADD (18)
#define BOTTOM_VIEW_HEIGHT ( BOTTOM_BTN_HEGIHT + BOTTOM_BTN_TOPADD * 2)

#define TITLE_COLOR (COLOR_HEXSTRING(@"#1f90e5"))

typedef NS_ENUM(NSInteger , Cell_Type)
{
    Cell_Type_OnView = 10,//cell上面一层蒙板view
    
};

@interface BingdingMyBoxCarViewController ()
{
    NSIndexPath * selectIndexPath;
}
@property (nonatomic , strong) UIButton * saveBtn;

@property (nonatomic , copy) NSArray <CarModel *>* carModelList;

@end

@implementation BingdingMyBoxCarViewController
- (instancetype)initWithVioCarModles:(NSArray<CarModel *> *)carModels
{
    self = [super initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    if (self) {
        self.carModelList = [NSArray arrayWithArray:carModels];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择我的车辆";

    selectIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
    
    [self createTableView];
    [self createUI];
}
- (void)createUI
{
    [[[UILabel labelWithFrame:CGRectMake(10, 0, __MAIN_WIDTH - 10 * 2, HEAD_VIEW_HEIGHT) font:FONT_DEFAULT_Bold(20) text:@"绑定盒子的是否为以下车辆？" textColor:COLOR_HEXSTRING(@"#3a3a3a")] setupOnView:self.view] labelResetTextAlignment:NSTextAlignmentCenter];
    
    CGFloat btnX = 25 * __MAIN_RATIO_375;
    __weak __typeof(self) wself = self;
    self.saveBtn = [[[[UIButton buttonWithFrame:CGRectMake(btnX , self.tableView.bottom + BOTTOM_BTN_TOPADD, __MAIN_WIDTH - btnX * 2, BOTTOM_BTN_HEGIHT) title:@"" textColor:TITLE_COLOR font:FONT_DEFAULT_Light(15) click:^{
        if(selectIndexPath.row >= 0)//设定为绑定车辆
        {
            CarModel * carModel = wself.carModelList[selectIndexPath.row];
            if (wself.Click) {
                wself.Click(carModel);
            }
        }else{
            if (wself.Click) {
                wself.Click(nil);
            }
        }
    }] setupOnView:self.view] resetBorderWidth:1 borderColor:TITLE_COLOR] resetCornerRadius:2];
    [self loadBtnTextColorIsWhite:NO];
}
- (void)loadBtnTextColorIsWhite:(BOOL)textIsWhite
{
    [self.saveBtn setTitleColor:(textIsWhite ? [UIColor whiteColor] : TITLE_COLOR) forState:UIControlStateNormal];
    [self.saveBtn setBackgroundColor:textIsWhite ? TITLE_COLOR : [UIColor whiteColor]];
    [self.saveBtn setTitle:(textIsWhite ?  @"是的，设为绑定车辆" : @"不是，保存为新车辆") forState:UIControlStateNormal];
}
- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, HEAD_VIEW_HEIGHT , __MAIN_WIDTH , __MAIN_HEIGHT_64 - BOTTOM_VIEW_HEIGHT - HEAD_VIEW_HEIGHT);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
}

#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carModelList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return [MineCarCell getCellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCarCell * cell;
    NSString * cellID = @"ViolationCarCellID";
    cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MineCarCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [cell.rightImageV setImage:[ImageHelper renderImage:ImageNamed(@"choiceCityRightImage") withColor:COLOR_RGBA(211, 211, 211, 1)]];
        
        [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, [MineCarCell getCellHeight])] resetBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.4]] setupOnView:cell].tag = Cell_Type_OnView;
        
    }
    UIView * onView = [cell viewWithTag:Cell_Type_OnView];
    if (selectIndexPath.row >= 0) {
        [onView setHidden:indexPath.row == selectIndexPath.row];
        
        [cell.rightImageV setImage:(indexPath.row == selectIndexPath.row ? ImageNamed(@"choiceCityRightImage") : [ImageHelper renderImage:ImageNamed(@"choiceCityRightImage") withColor:COLOR_RGBA(211, 211, 211, 1)])];
    }else
    {
        [onView setHidden:NO];
        [cell.rightImageV setImage:[ImageHelper renderImage:ImageNamed(@"choiceCityRightImage") withColor:COLOR_RGBA(211, 211, 211, 1)]];
    }
    
    [cell reloadDataModel:self.carModelList[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([Check isVisiter])
    {
        return;
    }
    if (selectIndexPath.section == indexPath.section && selectIndexPath.row == indexPath.row) {
        selectIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
    }else selectIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    [self.tableView reloadData];
    [self loadBtnTextColorIsWhite:selectIndexPath.row >= 0];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
